﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeiXinWeb.Server;
using WeiXinWeb.Models;
using WeiXinWeb.Helper;

namespace WeiXinWeb.Controllers
{
    public class ReservationController : Controller
    {
        //
        // GET: /Reservation/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult IncomView()
        {
            var incomlist = new Import_info().GetimportList().Where(a => a.isweixin == IsWXStatus.release & a.Status == "待派车" || a.Status == "待下达计划");
          //  var todaylist = incomlist.Where(a=>a.PlanDt.ToString("yyyy-MM-dd")==DateTime.Now.ToString("yyyy-MM-dd")).DistinctBy(a=>a.Address).ToList();
            var today = (from item in incomlist where item.PlanDt.ToShortDateString() == DateTime.Now.ToShortDateString() group item by item.Address into g select new { count = g.Count(), adress = g.Key }).ToList();
            var tomrrowlist = (from item in incomlist where item.PlanDt.ToShortDateString() == DateTime.Now.AddDays(1).ToShortDateString() group item by item.Address into g select new { count = g.Count(), adress = g.Key }).ToList();
            ViewBag.address = today;
            ViewBag.tomaddress = tomrrowlist;
            return View();
        }

        public ActionResult IncomRelateView(string md)
        {
            var incomlist = new Import_info().GetimportList().Where(a => a.isweixin == IsWXStatus.release&a.Status=="待派车"||a.Status=="待下达计划");
            var todaylist = incomlist.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")&a.Address==md).ToList();
            ViewBag.list = todaylist;
            return View();
        }

        public ActionResult IncomReserationView(string id)
        {
            //货运明细
            var cc = new Import_info().GetimportSinlg(new InConsignationDetail {Id=id });
            //拖车列表

            var vad = new TruckServer().Getcarlist(((TruckCompany)HelpCookies.CurrentUserObject).Id);

            ViewBag.vehicle = vad;

            return View(cc);
        }

        [HttpPost]
        public ActionResult Save(WeiXinRes model)
        {
            TruckCompany tc=(TruckCompany)HelpCookies.CurrentUserObject;

            //得到车信息

            var valice = new TruckServer().Getcarlist(tc.Id).Where(a=>a.Id==model.carnumber).Single();

            model.id = Guid.NewGuid();
            model.ResverTime = DateTime.Now;
            model.owner = tc.Name;
            model.ownerTel = tc.tel;
            model.carnumber = valice.TruckNumber;
            model.driverTel = valice.DriverTel;
            model.driver = valice.Driver;

            var result = new WeiXinResServer().InsertInfo(model);
            if (result)
            {
                return Json("");
            }
            else
            {
                return Json("信息操作失败！");
            }        
        }

        public ActionResult OutcomView()
        {
            var incomlist = new OutportServer().GetoutportList().Where(a => a.isweixin == IsWXStatus.release & a.Status == "待派车" || a.Status == "待下达计划");
            var list = (from item in incomlist where item.PlanDt.ToShortDateString() == DateTime.Now.ToShortDateString() group item by item.Address into g select new { count = g.Count(), adress = g.Key }).ToList();
            var tomrrowlist = (from item in incomlist where item.PlanDt.ToShortDateString() == DateTime.Now.AddDays(1).ToShortDateString() group item by item.Address into g select new { count = g.Count(), adress = g.Key }).ToList();
            ViewBag.address = list;
            ViewBag.tomaddress = tomrrowlist;
            return View();
        }

        public ActionResult OutcomRelateView(string md)
        {
            var incomlist = new OutportServer().GetoutportList().Where(a => a.isweixin == IsWXStatus.release & a.Status == "待派车" || a.Status == "待下达计划");
            var todaylist = incomlist.Where(a => a.PlanDt.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd") & a.Address == md).ToList();
            ViewBag.list = todaylist;
            return View();
        }

        public ActionResult OutcomReserationView(string id)
        {
            //货运明细
            var cc = new OutportServer().GetOutportSinlg(new OutConsignationDetail { Id = id });
            //拖车列表

            var vad = new TruckServer().Getcarlist(((TruckCompany)HelpCookies.CurrentUserObject).Id);

            ViewBag.vehicle = vad;

            return View(cc);   
        }

        public ActionResult SaveExport(WeiXinRes model)
        {
            TruckCompany tc = (TruckCompany)HelpCookies.CurrentUserObject;

            //得到车信息

            var valice = new TruckServer().Getcarlist(tc.Id).Where(a => a.Id == model.carnumber).Single();
            model.id = Guid.NewGuid();
            model.ResverTime = DateTime.Now;
            model.owner = tc.Name;
            model.ownerTel = tc.tel;
            model.carnumber = valice.TruckNumber;
            model.driverTel = valice.DriverTel;
            model.driver = valice.Driver;
            if (model.cntnum == "/")
            {
                model.cntnum = "";
            }

            var result = new WeiXinResServer().InsertInfout(model);
            if (result)
            {
                return Json("");
            }
            else
            {
                return Json("信息操作失败！");
            } 
        }

        public ActionResult ReservationDetil()
        {
            ViewBag.Import = new WeiXinResServer().GetImportReseration(((TruckCompany)HelpCookies.CurrentUserObject).tel);


            ViewBag.Outport = new WeiXinResServer().GetOutportReseration(((TruckCompany)HelpCookies.CurrentUserObject).tel);

            return View();
        }
        
    }
}
